#include <stdio.h>

#define LEN 8
int a[LEN] = { 1, 2, 2, 2, 5, 6, 8, 9 };

int binarySearch(int start, int end, int searchNum)
{
	int mid;
	// if(start <= end)
	// {
	// 	mid = (start + end)/2;
	// 	if(a[mid] == searchNum)
	// 		return mid;
	// 	else if(a[mid] > searchNum)
	// 		return binarySearch(start, mid-1, searchNum);
	// 	else
	// 		return binarySearch(mid +1, end, searchNum);
	// }
	while(start <= end)
	{
		mid = (start + end)/2;
		if(a[mid] == searchNum)
			{
				int i;
				// 向前找,找到第一个
				for(i = mid -1; i >= start && a[i] == searchNum ; i--);
				return i+1;
					
			}
			//return mid;
		else if(a[mid] > searchNum)
			end = mid -1;
		else
			start = mid +1;
	}
	return -1;
} 
int main(void)
{
	int searchNum = 2;
	int find = binarySearch(0, LEN-1, searchNum);
	printf("find = %d.\n",find);
	return 0;
}
